﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;

namespace WebApplication.Controls
{
    public partial class UC_DSSanPham : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PopulateControls();
            }
        }
        public void PopulateControls()
        {
            int howManyPages = 0;
            string chudeId = Request.QueryString["IDChuDe"] ?? "";
            string page = Request.QueryString["Page"] ?? "1";
            string firstPageUrl = "";
            string pagerFormat = "";
            if (chudeId != "")
            {
                List<Entities.SanPham> data = SanPhamBLL.SanPham_TrongChuDe(chudeId, page, out howManyPages);
                Session["GioHangASP"] = data;
                dlDSSanPham.DataSource = data;
                dlDSSanPham.DataBind();
                firstPageUrl = LinkBLL.ToChuDe(chudeId);
                pagerFormat = LinkBLL.ToChuDe(chudeId, "{0}");
            }
            else
            {
                List<Entities.SanPham> data = SanPhamBLL.SanPham_HienThi(page, out howManyPages);
                dlDSSanPham.DataSource = data;
                Session["GioHangASP"] = data;
                dlDSSanPham.DataBind();
                firstPageUrl = LinkBLL.ToChuDe("1");
                pagerFormat = LinkBLL.ToChuDe("{0}");
            }
            pagerBottom.Show(int.Parse(page), howManyPages, firstPageUrl, pagerFormat, false);

        }

        protected void btnTimKiem_Click(object sender, EventArgs e)
        {
            string tim = txtTimKiem.Text.ToLower();
            string tim1 = txtTimKiem.Text.ToLower();
            string tim2 = txtTimKiem.Text.ToLower();
            string tim3 = txtTimKiem.Text.ToLower();
            if (tim.Length > 0)
            {
                dlDSSanPham.DataSource = SanPhamBLL.TimTheoTen(tim);
                dlDSSanPham.DataBind();
            }
            else
                if (tim1.Length > 0)
                {
                    dlDSSanPham.DataSource = SanPhamBLL.TimTheoTenChuDe(tim1);
                    dlDSSanPham.DataBind();

                }
                else
                    if (tim2.Length > 0)
                    {
                        dlDSSanPham.DataSource = SanPhamBLL.TimTheoTenLoaiSP(tim2);
                        dlDSSanPham.DataBind();

                    }
                    else
                        if (txtGia1.Text.Length > 0 && txtGia2.Text.Length > 0)
                        {
                            dlDSSanPham.DataSource = SanPhamBLL.TimTheoGia(Double.Parse(txtGia1.Text), Double.Parse(txtGia2.Text));
                            dlDSSanPham.DataBind();
                        }
                        else

                            if (tim3.Length > 0 || txtGia1.Text.Length > 0 && txtGia2.Text.Length > 0)
                            {
                                dlDSSanPham.DataSource = SanPhamBLL.TimKiem(tim3, Double.Parse(txtGia1.Text), Double.Parse(txtGia2.Text));
                                dlDSSanPham.DataBind();
                            }
        }

        protected void dlDSSanPham_ItemCommand(object source, DataListCommandEventArgs e)
        {
            List<Entities.SanPham> data = Session["GioHangASP"] as List<Entities.SanPham>;
            int index = e.Item.ItemIndex;
            ChiTietGioHang _chiTiet = new ChiTietGioHang(data[index]);
            GioHang.TheHien.ThemChiTiet_GH(_chiTiet);
            Response.Redirect("~/HienThiGioHang.aspx");
        }
    }
}